import { PRIORITY_VIEW } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import { GListView } from "../../../Core/GView/GListView";
import GPage from "../../../Core/GView/GPage";
import { CMsg, FRONT, LISTENTTYPE, RPointMask } from "../../Common/Define";
import { Res } from "../../Common/UIResources";
import GameMgr from "../../Logic/GameMgr";
import { IHeroSort } from './../../Logic/HeroMgr';
import HeroCardItem from "./HeroCardItem";

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Hero/SquadCtrl')

export default class SquadCtrl extends GPage {

    private _rHeroDataBattle: number[] = null;
    onGLoad() {
        this.node.setContentSize(cc.winSize);
        GCtrl.ES.on(CMsg.client.home.onheroBattleItem, this, this.render.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.hero.onAddHero, this, this.initEmbattle.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.hero.onHeroLvUp, this, this.initEmbattle.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.hero.onHeroBreach, this, this.initEmbattle.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.hero.onHeroAddStar, this, this.initEmbattle.bind(this), PRIORITY_VIEW);
    }

    onPageIn() {
        this.initEmbattle();
        this._rHeroDataBattle = GameMgr.rHeroData.heroIdsInBattle;
        this.render();
        this.renderIcon();

    }
    _onTouchStar(event: cc.Event.EventTouch) {
        GCtrl.ES.emit(CMsg.client.home.onBattleItem, -1);
    }

    @property(cc.ScrollView) cardView: cc.ScrollView = null;
    @property(cc.Node) content: cc.Node = null;
    @property(cc.Node) mask: cc.Node = null;
    @property(cc.Node) battle: cc.Node = null;

    @property(cc.Node) battle1: cc.Node = null;

    @property(cc.Node) break: cc.Node = null;
    private _listView: GListView = null;
    private _curListIndex: number = 0;

    /**初始化View */
    protected initEmbattle() {
        this.break.on(cc.Node.EventType.TOUCH_START, this._onTouchStar, this);
        if (!this._listView) {
            this.assetImpl.prefab(Res.prefab.item.heroCardItem, (prefab: cc.Prefab) => {
                this._listView = new GListView({
                    scrollview: this.cardView,
                    mask: this.mask,
                    content: this.content,
                    itemTpl: cc.instantiate(prefab),
                    itemSetter: this.onItemSetter.bind(this),
                    childClick: this.childClick,
                    column: 3,
                    gapY: 20,
                    gapX: 28,
                    padingY: 20,
                    padingX: 18,
                    isCbClass: true,
                    isWidget: true,
                });
                this.dealWithData();
            })
        } else {
            this.dealWithData();
        }
    }

    protected dealWithData() {
        let allHero = GameMgr.rHeroData.getHaveHeros(this._curListIndex);
        let hava = allHero.have;
        let havaSort = [IHeroSort.sortLvUp, IHeroSort.sortStart, IHeroSort.sortZhanli];
        let sort = (a: IHeroData, b: IHeroData): number => {
            return IHeroSort.sorts(havaSort, a, b)
        }
        hava.sort(sort);
        this._listView.setData(hava);
    }

    /**上阵切页 */
    protected btnOnce(CustomEventData, index) {
        this._curListIndex = Number(CustomEventData.node._name);
        GCtrl.ES.emit(CMsg.client.home.onBattleItem, -1);
        this.dealWithData();
    }


    /**初始每个卡牌 */
    public onItemSetter(item: HeroCardItem, data: IHeroData, index: number) {
        item.setSeatNode(this.battle1);
        item.init(data, index, LISTENTTYPE.listent1);
    }


    protected render() {
        for (let index = 0; index < this._rHeroDataBattle.length; index++) {
            if (Number(this._rHeroDataBattle[index]) != -1) {
                this.battle.children[index].getChildByName('prepareBattle').active = true;
                let sprite = this.battle.children[index].getChildByName('prepareBattle').children[0].getComponent(cc.Sprite);
                // this.assetImpl.spriteAtlasFrame(sprite, atlas, TEXTURE_PREFIX.skill + '100   103');
                this.assetImpl.spriteFrame(sprite, Res.texture.hero.hero + this._rHeroDataBattle[index]);
            } else {
                this.battle.children[index].getChildByName('prepareBattle').active = false;
            }
        }
        GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Go_To_Battle, false);
    }

    protected renderIcon() {
        for (let index = 0; index < this._rHeroDataBattle.length; index++) {
            if (this._rHeroDataBattle[index] != -1) {
                this.battle.children[index].getChildByName('prepareBattle').active = true;
                // let sprite = this.battle.children[index].getChildByName('prepareBattle').children[0].getComponent(cc.Sprite);
                // this.assetImpl.spriteAtlasFrame(sprite, atlas, TEXTURE_PREFIX.skill + '100103');// ID.toString()
            } else {
                this.battle.children[index].getChildByName('prepareBattle').active = false;
            }
        }
    }

    protected childClick(item: HeroCardItem, data: SHeroDataRaw, index: number) {

    }


    /**上下阵 */
    protected isBattle(CustomEventData, index) {
        this.brush(Number(index));
    }

    protected brush(id) {
        let bool = false;
        let rHeroData = GameMgr.rHeroData.heroIdsInBattle;
        if (rHeroData[id] != -1) {
            GCtrl.ES.emit(CMsg.client.home.onHeroItem, [FRONT.Lower, rHeroData[id]]);
            rHeroData[id] = -1;
            GCtrl.ES.emit(CMsg.client.home.onheroBattleItem);
        }
        GameMgr.rHeroData.heroIdsInBattle = rHeroData;
    }

    protected oneKey() {
        let rHeroData = GameMgr.rHeroData.heroIdsInBattle;
        let allHero = GameMgr.rHeroData.getHaveHeros(this._curListIndex);
        let hava = allHero.have;
        let havaSort = [IHeroSort.sortLvUp, IHeroSort.sortStart, IHeroSort.sortZhanli];
        let sort = (a: IHeroData, b: IHeroData): number => {
            return IHeroSort.sorts(havaSort, a, b)
        }
        hava.sort(sort);
        let arr = [];
        rHeroData.forEach((v, index) => {
            if (v == -1) {
                arr.push(index);
            }
            if (index == rHeroData.length - 1) {
                if (arr.length >= hava.length) {
                    arr.forEach((v: number, index) => {
                        if (hava[index]) {
                            rHeroData[v] = hava[index].tableId;
                            GCtrl.ES.emit(CMsg.client.home.onHeroItem, [FRONT.battle, hava[index].tableId]);
                            GCtrl.ES.emit(CMsg.client.home.onheroBattleItem);
                        }
                    })
                } else {
                    arr.forEach((v: number, index) => {
                        rHeroData[v] = hava[index].tableId;
                        GCtrl.ES.emit(CMsg.client.home.onHeroItem, [FRONT.battle, hava[index].tableId]);
                        GCtrl.ES.emit(CMsg.client.home.onheroBattleItem);
                    })
                }
            }
        })

        GameMgr.rHeroData.heroIdsInBattle = rHeroData;
    }
}